﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace pdftoswf
{
    internal class Program
    {
        public static void Main(string[] args)
        {

            string strToolPath = ConfigurationSettings.AppSettings["SWFTools"].ToString();
            string strDOCPath = ConfigurationSettings.AppSettings["DOCPath"].ToString();
            string strPDFAndSWFPath = ConfigurationSettings.AppSettings["DOCPath"].ToString();

            DirectoryInfo WORD = new DirectoryInfo(strDOCPath);
            FileInfo[] files = WORD.GetFiles("*.doc*", SearchOption.AllDirectories);

            foreach (var item in files)
            {
                if (File.Exists(item.DirectoryName + "\\" + item.Name.Replace("doc", "swf")))
                {

                }
                else
                {
                    try
                    {
                        DocToSWF(item.DirectoryName, item.DirectoryName, item.Name, strToolPath);
                    }
                    catch (Exception ex)
                    {

                        Console.WriteLine(ex.ToString());
                        Console.ReadKey();
                        continue;
                    }
                }
            }




        }

        //WORD转PDF
        public static bool DocToSWF(string strDOCPath, string strPDFAndSWFPath, string fileName, string strToolPath)
        {
            string strDOCFullName = strDOCPath + "\\" + fileName;
            string strPDFFullName = strPDFAndSWFPath + "\\" + fileName;

            WPS.Application WPSApp = new WPS.Application();
            string strDoc = strDOCFullName;
            string strpdf = "";
            if (strPDFFullName.Contains(".docx"))
            {
                strpdf = strPDFFullName.Replace("docx", "pdf");

            }
            else
            {
                strpdf = strPDFFullName.Replace("doc", "pdf");
            }
            var doc = WPSApp.Documents.Open(strDoc);
            int page = doc.ActiveWindow.ActivePane.Pages.Count;
            doc.ExportPdf(strpdf);
            object MissingValue = Type.Missing;
            object bSave = false;
            if (WPSApp != null)
            {
                // 首先调用WPS.Application的Quit方法，并且不保存修改
                WPSApp.Quit(ref bSave, ref MissingValue, ref MissingValue);
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(WPSApp);
            WPSApp = null;
            ConvertToSwf(strpdf, strToolPath);
            //File.Move(strDOCFullName, strPDFAndSWFPath + "\\" + fileName);
            return true;
        }

        //PDF转SWF
        private static void ConvertToSwf(string pdfPath, string pdfswfurl)
        {
            try
            {
                string exe = pdfswfurl;
                if (!File.Exists(exe))
                {
                    throw new ApplicationException("Can not find: " + exe);
                }

                string swfPath = pdfPath.Replace("pdf", "swf");

                StringBuilder sb = new StringBuilder();
                sb.Append(" -o \"" + swfPath + "\"");//output
                sb.Append(" -z");
                sb.Append(" -s flashversion=9");//flash version
                //sb.Append(" -s disablelinks");//禁止PDF里面的链接
                sb.Append(" \"" + pdfPath + "\"");//input
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo.FileName = exe;
                proc.StartInfo.Arguments = sb.ToString();
                proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                proc.Start();
                proc.WaitForExit();
                proc.Close();
                //删除PDF
                File.Delete(pdfPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}